\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mymath}[2015/01/09 Math environments package]



% Math packages
\RequirePackage{amsmath, amsfonts, amssymb, mathrsfs, bbm, pifont, breqn}
\RequirePackage{enumitem} % Fancy enumerations
\RequirePackage{textcomp}
% The ifthenelse macro used here internally
\RequirePackage{ifthen}
% Indexing
%\RequirePackage{makeidx}
% Framed environment used for frames around theorems
\RequirePackage{framed}
% My package used for breaking math
\RequirePackage{mathbreaks}



% Commands used internally for the theorem box
\newcommand{\theoremboxbegin}{\relax}
\newcommand{\theoremboxend}{\relax}

% Proof nesting counter
\newcounter{nestedproof}
\setcounter{nestedproof}{0}


% Usage: adds a frame around theorems
\DeclareOption{framedtheorems}{
  \renewcommand{\theoremboxbegin}{\begin{minipage}{\textwidth}\begin{framed}}
  \renewcommand{\theoremboxend}{\end{framed}\end{minipage}}
}

% Usage: just places theorems in a separate box
\DeclareOption{boxedtheorems}{
  \renewcommand{\theoremboxbegin}{\begin{minipage}{\textwidth}}
  \renewcommand{\theoremboxend}{\end{minipage}}
}

\ProcessOptions\relax



%% Internal styling macros that can be overriden for customization %%

% 'Theorem' keyword
\newcommand{\theoremkw}{Теорема.}

% 'Theorem' keyword style
\newcommand{\theoremkwstyle}{\bfseries}

% Theorem name style
\newcommand{\theoremnamestyle}{\bfseries\itshape}

% Theorem text style
\newcommand{\theoremtextstyle}{\sffamily}


% 'Lemma' keyword
\newcommand{\lemmakw}{Лемма.}

% 'Lemma' keyword style
\newcommand{\lemmakwstyle}{\bfseries}

% Lemma text style
\newcommand{\lemmatextstyle}{\normalfont\sffamily}


% 'Consequence #n' keyword
\newcommand{\consequencekw}{Следствие }

% 'Consequence #n' keyword style
\newcommand{\consequencekwstyle}{\itshape}


% 'Definition' keyword
\newcommand{\definitionkw}{Определение.}

% 'Definition' keyword style
\newcommand{\definitionkwstyle}{\bfseries}

% Definition text style
\newcommand{\definitiontextstyle}{\normalfont}


% 'Note' keyword
\newcommand{\notekw}{Замечание.}

% 'Note' text style
\newcommand{\notekwstyle}{\itshape}

% Note text style
\newcommand{\notetextstyle}{\normalfont}


% 'Proof' keyword
\newcommand{\proofkw}{Доказательство.}

% 'Proof' keyword style
\newcommand{\proofkwstyle}{\itshape}

% 'Partial proof' keyword style
\newcommand{\partialproofkwstyle}{\bfseries}

% QED symbol
\newcommand{\qedsymbol}{\ensuremath{\blacksquare}}


% 'Left-proof' keyword
\newcommand{\leftproofkw}{Докажем справа налево}

% 'Right-proof' keyword
\newcommand{\rightproofkw}{Докажем слева направо}

% 'Proof of necessity' keyword
\newcommand{\necessityproofkw}{Докажем необходимость.}

% 'Proof of sufficiency' keyword
\newcommand{\sufficiencyproofkw}{Докажем достаточность.}

% 'Left-proof' and 'Right-proof' keyword style
\newcommand{\halfproofkwstyle}{\textsl}

% A QED symbol for a nested proof.
\newcommand{\nestedqedsymbol}{\ensuremath{\square}}


% Latin text style
\newcommand{\latin}[1]{\textsl{#1}}

%%% -- %%%

\newcommand{\qedselect}{
  \ifnum\value{nestedproof}=0
    \qedsymbol
  \else
    \nestedqedsymbol
  \fi
}

\newcommand{\apst}{\latin{a posteriori}}
\newcommand{\apr}{\latin{a priori}}

\newcommand{\defined}[2][]{%
  \ifx&#1&%
    \relax%
  \else%
    \index{#1}%
  \fi%
  \emph{#2}%
}

\newcommand{\definition}[2][]{
  \ifx&#1&
    \relax
  \else
    \index{#1}
  \fi
  \par
  \noindent{\definitionkwstyle\definitionkw}\space
  {
    \definitiontextstyle #2
  }
}


\newenvironment{consequences}[1][]{%
  \begin{enumerate}[label={\consequencekwstyle \consequencekw} \arabic*.\space, align=left]
}{
  \end{enumerate}
}


\newcommand{\note}[1]{
  \par
  \noindent{\notekwstyle\notekw}\space
  {
    \notetextstyle #1
  }
  \par
}


\def\formattedtheoremname{\relax}
\newenvironment{theorem}[1][]{
  % Parameters are not allowed in END section, so process them here.
  % The name is enclosed in parenthesis and in a box, so we define it already
  % formatted, so that we'll just have to insert the box and not to deal with
  % empty box and parenthesis.
  \ifx&#1&
    \def\formattedtheoremname{\relax}
  \else
    \index{#1}\def\formattedtheoremname{
      \parbox{\textwidth}{
        \raggedleft (\theoremnamestyle #1)
      }
    }
  \fi
  \resetmathbreak
  \par
  \noindent
    \theoremboxbegin
    \noindent{\theoremkwstyle\theoremkw}\space
    \begingroup
      \theoremtextstyle
}{
    \endgroup

    \formattedtheoremname
    \theoremboxend
}


\newenvironment{lemma}[1][]{
  \ifx&#1&
    \def\formattedtheoremname{\relax}
  \else
    \index{#1}\def\formattedtheoremname{
      \parbox{\textwidth}{
        \raggedleft (\theoremnamestyle #1)
      }
    }
  \fi
  \resetmathbreak
  \par
  \noindent
    \theoremboxbegin
    \noindent{\lemmakwstyle\lemmakw}\space
    \begingroup
      \lemmatextstyle
}{
    \endgroup

    \formattedtheoremname
    \theoremboxend
}


\newenvironment{proof}[1][]{%
  \ifx&#1&
    \par\noindent{\proofkwstyle\proofkw}\space
  \else
    \par\noindent{\proofkwstyle #1}\space
  \fi
  \stepcounter{nestedproof}
}{
  \addtocounter{nestedproof}{-1}
  \hspace*{\fill}\qedselect\par
  \vspace{0.5em}
}

\newenvironment{proof*}[1][]{%
  \ifx&#1&
    \par\noindent{\partialproofkwstyle\proofkw}\space
  \else
    \par\noindent{\partialproofkwstyle #1}
  \fi
}{
  \par
  \vspace{0.3em}
}

\newenvironment{leftproof}[1][]{
  \ifx&#1&
    \begin{proof*}[\leftproofkw.]
  \else
    \begin{proof*}[\leftproofkw:]
    предположим, что #1.
    \par
  \fi
}{
  \end{proof*}
}

\newenvironment{rightproof}[1][]{
  \ifx&#1&
    \begin{proof*}[\rightproofkw.]
  \else
    \begin{proof*}[\rightproofkw:]
    предположим, что #1.
    \par
  \fi
}{
  \end{proof*}
}

\newenvironment{necessityproof}{
  \begin{proof*}[\necessityproofkw]
}{
  \end{proof*}
}

\newenvironment{sufficiencyproof}{
  \begin{proof*}[\sufficiencyproofkw]
}{
  \end{proof*}
}

\endinput
